/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bground.sprites;

import bground.game.GL;
import bground.game.GL.Direction;
import bground.game.GameField;
import external.golden.gamedev.object.sprite.AdvanceSprite;
import java.awt.image.BufferedImage;

/**
 *
 * @author s6_kustan_e
 */
public class ExtendedSprite extends AdvanceSprite {

    public Direction direction;
    double targetX;
    double targetY;
    boolean isMoving = false;
    boolean moveable = false;
    double speed;
    GameField game;
    String name;

    public ExtendedSprite(String name, GameField game) {
        super();
        this.name = name;
        this.game = game;
        direction = Direction.up;
        speed = 0;
        lives = 1;
        BufferedImage[] bis = new BufferedImage[1];
        bis[0] = GL.getGraphics(name);
        setImages(bis);
    }

    public void move(GL.Direction where) {
        if (!moveable) {
            return;
        }
        if (!isMoving) {
            if (direction != where) {
                direction = where;
            } else {
                move(where, 50);
            }
        }
    }

    public void move(Direction where, double distance) {
        if (!moveable) {
            return;
        }
        if (!isMoving) {
            stopAndCenter();
            BufferedImage[] bis = new BufferedImage[1];
            bis[0] = GL.getGraphics(name, where);
            setImages(bis);
            isMoving = true;
            direction = where;
            targetX = getX();
            targetY = getY();


            switch (where) {
                case no:
                    isMoving = false;
                    break;
                case up:
                    targetY -= distance;
                    break;
                case down:
                    targetY += distance;
                    break;
                case left:
                    targetX -= distance;
                    break;
                case right:
                    targetX += distance;
                    break;
            }
        }
    }
    int lives;

    public int getLives() {
        return lives;
    }

    public void removeLive() {
        lives--;
        if (lives == 0) {
            setActive(false);
        }
    }

    public void fire() {
        if (isActive()) {
            Bullet b = new Bullet(this, game);
            System.out.println("" + this.getX() + " " + this.getY());
            game.addBullet(b);
        }
    }

    @Override
    protected void updateMovement(long l) {
        super.updateMovement(l);


        if (isMoving) {

            if (direction == Direction.up && getY() <= targetY
                    || direction == Direction.down && getY() >= targetY
                    || direction == Direction.left && getX() <= targetX
                    || direction == Direction.right && getX() >= targetX) {
                stopAndCenter();


            } else {
                switch (direction) {
                    case up:
                        setSpeed(0, -speed);


                        break;


                    case left:
                        setSpeed(-speed, 0);


                        break;


                    case down:
                        setSpeed(0, speed);


                        break;


                    case right:
                        setSpeed(speed, 0);


                        break;


                }

            }
        }
    }

    public void stopAndCenter() {
        setSpeed(0, 0);
        isMoving = false;
        setLocation(
                Math.round(getX() / 50) * 50, Math.round(getY() / 50) * 50);

    }
}
